<?php
class ScenarioController extends Zend_Controller_Action
{

    public function init()
    {
        Zend_Loader::loadClass('My_Graph');
        Zend_Loader::loadClass('My_Link');
        Zend_Loader::loadClass('My_Wertex');
        Zend_Loader::loadClass('My_Object');

        $this->graph  = new My_Graph($this->_getParam('graph', 1));
        $this->view->graph = $this->graph;
        
        $this->vertexId = $this->_getParam('id', $this->graph->getFirst());
        $this->addType = $this->_getParam('type');
    }

    public function indexAction()
    {
        $link = new My_Link();
        $links = $link->readByStart($this->vertexId);
        if(!count($links)){
            $links = $link->readByStart($this->graph->getFirst());
        }
        $this->view->links = $links;
    }

    public function deleteAction()
    {
        if($this->vertexId > 0){
            $wertex = new My_Wertex($this->vertexId);
            $wertex->delete();

            $link = new My_Link();
            $result = $link->readByEnd($this->vertexId);
            foreach($result AS $row){
                $link = new My_Link($row);
                $link->delete();
            }
        }
        $this->_forward('index', 'scenario', null, array('graph' => '1','message' => $msg));
    }

    public function addAction() {
        $form = new Default_Form_Module();

        $submit = $this->_getParam('submit');
        if(!isset($submit)){
            $this->view->form = $form;
            return true;
        }

        if ($this->_request->isPost()) {
            $values = $this->_request->getPost();
            // prepare values
            foreach (array('weight', 'object') AS $column) {
                $data[$column] = $values[$column];
            }
            
            if ($form->isValid($values)) {
                //create child node
                $wertex = new My_Wertex();
                $link = new My_Link();
                $link->setStart($this->vertexId);

                $childVertexId = $wertex->insert($values);
                if($childVertexId){
                    $link->setEnd($childVertexId);
                    if($link->insert()){
                        $msg = 'Veiksmīgi pievienots';
                    }
                }
                $this->_forward('index', null, null, array('message' => $msg));
            } else {
                $this->view->form = $form->populate($values);
            }
        }
    }

    public function weightAction() {
        $editId = $this->_getParam('id');
        $value = $this->_getParam('value');

        // load row data and save to form
        if ($this->_request->isPost()) {
            if (!empty($editId)) {
                $table = new Default_Model_DbTable_Link();
                $result = $table->find($editId);
                $data = $result->toArray();

                $where = $table->getAdapter()->quoteInto($table->_primary[1].' = ?', $editId);
                $table->update(array('weight' => $data[0]['weight'] + $value), $where);
            }
        }

        $this->_redirect('scenario/index/graph/'.$this->_getParam('graph'));
    }
    
}